package com.dingding.order.facade;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dto.FacadeResult;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.exception.bussiness.BussinessException;
import com.dingding.order.core.cancel.biz.ICancelOrderService;
import com.dingding.order.core.common.IUserOrderDao;
import com.dingding.order.core.common.IUserTaxiOrderDao;
import com.dingding.order.core.confirm.biz.IConfirmArriveService;
import com.dingding.order.core.distance.biz.ICalculateDistanceService;
import com.dingding.order.core.grab.biz.IGrabOrderService;
import com.dingding.order.core.myorder.biz.IGetHistoryOrderDetailService;
import com.dingding.order.core.myorder.biz.IGetHistoryOrderService;
import com.dingding.order.core.myorder.biz.IGetMyOrderService;
import com.dingding.order.core.orderlist.biz.IGetNearbyOrderListService;
import com.dingding.order.core.pay.biz.IGetPayResultService;
import com.dingding.order.core.pay.biz.IGoToPayService;
import com.dingding.order.core.pay.biz.IOrderPayService;
import com.dingding.order.core.pay.biz.IPayNotifyService;
import com.dingding.order.core.phonecall.service.IPhoneCallService;
import com.dingding.order.core.publish.biz.IPublishOrderByTaxiService;
import com.dingding.order.core.publish.biz.IPublishOrderService;
import com.dingding.order.core.publish.biz.IPublishSpecialOrderService;
import com.dingding.order.core.publish.biz.IPublishTaxiOrderService;
import com.dingding.order.core.syncorder.biz.ISyncOrderService;
import com.dingding.order.dto.pay.menums.TransactionTypeEnum;
import com.dingding.order.dto.phonecall.ReqGetPhoneCall;
import com.dingding.order.dto.phonecall.ResGetPhoneCall;
import com.dingding.order.dto.publish.taxi.ReqPublishOrderByTaxi;
import com.dingding.order.dto.publish.taxi.ResPublishOrderByTaxi;
import com.dingding.order.service.IUserOrderFacade;

/**
 * @ClassName: UserOrderFacade
 * @Description:订单服务接口实现类
 * @author YHQ
 * @date 2016年6月2日 下午7:04:55
 */

@Component("userOrderFacade")
public class UserOrderFacade<UserGrabOrderDTO, ReleaseOrderDTO> implements IUserOrderFacade {
	
	private Logger log = Logger.getLogger(this.getClass());

	@Resource(name = "orderPayService")
	private IOrderPayService orderPayService; // 订单支付接口

	@Resource(name = "taxiOneLineOrderPayService")
	private IOrderPayService taxiOrderPayService; // 出租车订单支付业务
	
	@Resource(name = "taxiOneLineOrderPayServiceV2")
	private IOrderPayService taxiOneLineOrderPayServiceV2; // 出租车订单支付业务

	@Resource(name = "payNotifyService")
	private IPayNotifyService payNotifyService;// 第三方支付成功，回调操作业务
	
	@Resource(name = "payNotifyServiceV2")
	private IPayNotifyService payNotifyServiceV2;

	@Resource(name = "distanceService")
	private ICalculateDistanceService calculateDistanceService; // 计算距离价格接口

	@Resource(name = "taxiDistanceService")
	private ICalculateDistanceService taxiCalculateDistanceService;// 出租车计算距离

	@Resource(name = "specialCalculateDistanceService")
	private ICalculateDistanceService specialCalculateDistanceService;// 专车计算距离
	
	@Resource(name = "getNearOrderList")
	private IGetNearbyOrderListService getNearbyOrderListService; // 获取附近订单列表接口

	@Resource(name = "taxiGetNearbyOrderListService")
	private IGetNearbyOrderListService taxiGetNearbyOrderListService;// 出租车获取附近订单列表接口

	@Resource(name = "confirmArriveService")
	private IConfirmArriveService confirmArriveService; // 确认到达逻辑

	@Resource(name = "taxiConfirmArriveService")
	private IConfirmArriveService taxiConfirmArriveService; // 确认到达逻辑

	@Autowired
	private IPublishOrderService publishOrderService;// 发布订单订单接口

	@Resource(name = "grabOrderService")
	private IGrabOrderService grabOrderService;

	@Resource(name = "taxiGrabOrderService")
	private IGrabOrderService taxiGrabOrderService;

	@Resource(name = "cancelOrderService")
	private ICancelOrderService cancelOrderService;

	@Resource(name = "taxiCancelOrderService")
	private ICancelOrderService taxiCancelOrderService;

	@Resource(name = "publishTaxiOrderService")
	private IPublishTaxiOrderService publishTaxiOrderService;
	
	@Resource(name = "publishOrderByTaxiService")
	private IPublishOrderByTaxiService publishOrderByTaxiService;

	@Resource(name = "getMyOrderService")
	private IGetMyOrderService getMyOrderService;
	
	@Resource(name = "getHistoryOrderService")
	private IGetHistoryOrderService getHistoryOrderService;
	
	@Resource(name = "getHistoryOrderDetailService")
	private IGetHistoryOrderDetailService getHistoryOrderDetailService;
	
	@Resource(name = "userOrderCommonDao")
	private IUserOrderDao userOrderDao;

	@Resource(name = "userTaxiOrderDao")
	private IUserTaxiOrderDao userTaxiOrderDao;

	@Resource(name = "goToPayService")
	private IGoToPayService goToPayService;

	@Resource(name = "getPayResultService")
	private IGetPayResultService getPayResultService;
	
	@Resource(name = "syncOrderService")
	private ISyncOrderService syncOrderService;

	@Resource(name="publishSpecialOrderService")
	private IPublishSpecialOrderService publishSpecialOrderService;
	
	@Resource(name="specialCancelOrderService")
	private ICancelOrderService specialCancelOrderService;
	
	@Resource(name = "phoneCallService")
	private IPhoneCallService phoneCallService;
	
	/**
	 * 
	 * @Title: releaseOrder
	 * @Description: 发布订单
	 * @author YHQ
	 * 
	 */
	@Override
	public String releaseOrder(String requestData) {

		return publishOrderService.publishOrder(requestData);
	}

	/**
	 * 
	 * @Title: UserGrabOrder
	 * @Description: 用户抢单
	 * @author YHQ
	 * 
	 */
	@Override
	public String userGrabOrder(String requestData) {

		return grabOrderService.userGrabOrder(requestData);
	}

	/**
	 * 
	 * @Title: taxiGrabOrder
	 * @Description: 出租车抢单
	 * @author YHQ
	 * 
	 */
	@Override
	public String taxiGrabOrder(String requestData) {

		return taxiGrabOrderService.userGrabOrder(requestData);
	}

	/**
	 * 
	 * @Description:乘客支付
	 * @author fengshuonan
	 * @since V2.2.0
	 * 
	 */
	@Override
	public String orderPay(String requestData) {
		return this.orderPayService.orderPay(requestData);
	}

	/**
	 * 
	 * @Description:出租车乘客线上支付
	 * @author CHEHUANBO
	 * @since V2.3.0 
	 * 
	 */
	@Override
	@Deprecated
	public String taxiOrderOnLinePay(String requestData) {
		return this.taxiOrderPayService.orderPay(requestData);
	}
	
	/**
	 * 
	 * @Description:出租车乘客线上支付
	 * @author CHEHUANBO
	 * @since V2.3.0 
	 * 
	 */
	@Override
	public String taxiOrderOnLinePayV2(String requestData) {
		return this.taxiOneLineOrderPayServiceV2.orderPay(requestData);
	}

	/**
	 * 
	 * @Description:出租车乘客线下支付
	 * @author CHEHUANBO
	 * @since V2.3.0 
	 * 
	 */
	@Override
	@Deprecated
	public String taxiOrderLinePay(String requestData) {
		return this.taxiOrderPayService.orderLinePay(requestData);
	}
	
	/**
	 * 
	 * @Description:出租车乘客线下支付
	 * @author CHEHUANBO
	 * @since V2.3.0 
	 * 
	 */
	@Override
	public String taxiOrderLinePayV2(String requestData) {
		return this.taxiOneLineOrderPayServiceV2.orderLinePay(requestData);
	}

	/**
	 * 
	 * 出租车订单，第三方平台支付成功后，更改订单状态
	 * 
	 * @Title: taxiOrderPayNotify 
	 * @param taxiOrderId 订单ID 
	 * @return boolean true 成功 false 失败
	 * @author CHEHAUNBO
	 * @since V2.3.0
	 * 
	 */
	@Override
	public boolean taxiOrderPayNotify(String taxiOrderId) {
		return this.payNotifyServiceV2.paySuccessUpdateOrderStatus(taxiOrderId, TransactionTypeEnum.onLine.getIndex(),
				true);
	}

	/**
	 * @Title: calculateDistance
	 * @Description:计算距离价格
	 * @author zhouxuan
	 */
	@Override
	public String calculateDistance(String requestData) {
		return this.calculateDistanceService.calculateDistance(requestData);
	}

	/**
	 * 
	 * @Title: taxiCalculateDistance
	 * @Description:出租车计算距离
	 * @author zhouxuan
	 * 
	 */
	@Override
	public String taxiCalculateDistance(String requestData) {
		return this.taxiCalculateDistanceService.calculateDistance(requestData);
	}

	/**
	 * 
	 * @Title: specialCalculateDistance 
	 * @Description: 专车计算距离价格
	 * @param requestData 客户端请求参数
	 * @author YHQ 
	 * @since V2.4.0
	 * @return String    返回类型
	 * 
	 */
	@Override
	public String specialCalculateDistance(String requestData){
		return specialCalculateDistanceService.calculateDistance(requestData);
	}
	
	/**
	 * @Title: getNearOrderList
	 * @Description: 获取附近订单列表
	 * @author zhouxuan
	 */
	@Override
	public String getNearOrderList(String requestData) {
		return this.getNearbyOrderListService.getNearOrderList(requestData);
	}

	/**
	 * @Title: taxiGetNearbyOrderList
	 * @Description:出租车获取附近订单列表
	 * @author zhouxuan
	 * @since V2.3.0
	 */
	public String taxiGetNearbyOrderList(String requestData) {
		return this.taxiGetNearbyOrderListService.getNearOrderList(requestData);
	}

	/**
	 * @Description: 确认到达请求bean
	 * @author YHQ
	 * @return String
	 */
	@Override
	public String confirmArrive(String requestData) {

		return confirmArriveService.confirmArrive(requestData);
	}

	/**
	 * @Description: 确认到达请求bean
	 * @param confirmArriveReqDTO
	 * @author YHQ
	 * @version V2.3.0
	 * @return String
	 */
	@Override
	public String taxiConfirmArrive(String requestData) {

		return taxiConfirmArriveService.confirmArrive(requestData);
	}

	/**
	 * 
	 * @Description：取消订单服务
	 * @param confirmArriveReqDTO
	 * @author CHEHUANBO
	 * @return String
	 * 
	 */
	@Override
	public String cancelOrder(String requestData) {

		return cancelOrderService.cancelOrder(requestData);
	}

	/**
	 * 
	 * @Description：取消订单服务
	 * @param taxiCancelOrder
	 * @author CHEHUANBO
	 * @return String
	 * 
	 */
	@Override
	public String taxiCancelOrder(String requestData) {

		return taxiCancelOrderService.cancelOrder(requestData);
	}

	/**
	 * 
	 * @Title: autoConfirmArrive
	 * @Description: 自动确认到达
	 * @author YHQ
	 * @return void 返回类型
	 * 
	 */
	@Override
	public void autoConfirmArrive() {

		confirmArriveService.automaticConfirmArrive();
	}

	/**
	 * 
	 * @Title: autoCancelOrder
	 * @Description: 自动取消订单
	 * @author YHQ
	 * @return void 返回类型
	 * 
	 */
	@Override
	public void autoCancelOrder() {

		cancelOrderService.autoCancelOrder();
	}

	/**
	 * 
	 * @Title: autoCancelTaxiOrder
	 * @Description: 自动取消出租车订单
	 * @author YHQ
	 * @since V2.3.0
	 * @return void 返回类型
	 * 
	 */
	@Override
	public void autoCancelTaxiOrder() {

		taxiCancelOrderService.autoCancelOrder();
	}
	
	/**
	 * 
	 * @Title: autoCancelSpecialOrder 
	 * @Description:自动取消专车订单 
	 * @author SUNZHICHENG  
	 * @date  2016年9月6日 上午9:30:46
	 * @since v2.4.0
	 */
	@Override
	public void autoCancelSpecialOrder() {
		
		specialCancelOrderService.autoCancelOrder();
	}

	/**
	 * @Description: 发布出租车订单
	 * @author fengshuonan
	 * @return void
	 */
	@Override
	public String publishTaxiOrder(String requestData) {

		return this.publishTaxiOrderService.publishTaxiOrder(requestData);
	}

	/**
	 * @Description: 发布出租车订单
	 * @param reqPublishOrderByTaxi 客户端请求参数
	 * @author YHQ
	 * @since V2.5.6
	 * @return void
	 */
	@Override
	public ResponseEntity<ResPublishOrderByTaxi> publishOrderByTaxi(ReqPublishOrderByTaxi reqPublishOrderByTaxi) {

		return this.publishOrderByTaxiService.publishOrderByTaxi(reqPublishOrderByTaxi);
	}
	
	/**
	 * @Title: getMyOrder
	 * @Description:我的订单
	 * @author zhouxuan
	 * @since V2.3.0
	 */
	public String getMyOrder(String requestData) {
		return this.getMyOrderService.getMyOrder(requestData);
	}

	/**
	 * @Title: getHistoryOrder
	 * @Description:历史订单
	 * @author YHQ
	 * @since V2.5.0
	 */
	public String getHistoryOrder(String requestData){
		return this.getHistoryOrderService.getHistoryOrders(requestData);
	}
	
	/**
	 * @Title: getHistoryOrderDetail
	 * @Description:历史订单详情
	 * @author YHQ
	 * @since V2.5.0
	 */
	public String getHistoryOrderDetail(String requestData){
		return this.getHistoryOrderDetailService.getHistoryOrderDetail(requestData);
	}
	
	/**
	 * @Description: 获取订单状态(常规订单)
	 * @param orderId 订单id
	 * @author fengshuonan
	 * @return Integer 订单状态
	 * @since 2.3.0
	 */
	@Override
	public Integer getUserOrderStatus(String orderId) {

		return userOrderDao.getUserOrderStatus(orderId);
	}

	/**
	 * @Description:获取订单状态(出租车订单)
	 * @param orderId 订单id
	 * @author fengshuonan
	 * @return Integer 订单状态
	 * @since 2.3.0
	 */
	@Override
	public Integer getTaxiUserOrderStatus(String orderId) {

		return userTaxiOrderDao.getUserTaxiOrderStatus(orderId);
	}

	/**
	 * @Description: 去支付接口
	 * @author fengshuonan
	 * @since 2.3.0
	 */
	@Override
	public String goToPay(String requestData) {

		return this.goToPayService.goToPay(requestData);
	}

	/**
	 * 
	 * @Title: queryPayResult 
	 * @Description: 查询支付结果的状态
	 * @param requestData 客户端请求参数
	 * @author YHQ 
	 * @since V2.3.0
	 * @return String    返回类型
	 * 
	 */
	@Override
	public String queryPayResult(String requestData) {

		return getPayResultService.getPayResult(requestData);
	}

	/**
	 * @Description:同步订单接口
	 * @author fengshuonan
	 * @return void
	 * @since 2.3.2
	 */
	@Override
	public void syncOrder() {
		this.syncOrderService.syncOrder();
	}

	/**
	 * @Description: 发布专车订单
	 * @author YHQ
	 * @since V2.4.0
	 * @return void
	 */
	@Override
	public String publishSpecialOrder(String requestData) {
		
		return publishSpecialOrderService.publishOrder(requestData);
	}
	
	/**
	 * 
	 * @Description: 取消专车订单请求bean
	 * @param specialCancelOrder
	 * @author YHQ
	 * @return String
	 * 
	 */
	@Override
	public String specialCancelOrder(String requestData) {

		return specialCancelOrderService.cancelOrder(requestData);
	}

	/**
	 * @Description:获取被拨打的号码
	 * @author fengshuonan
	 * @since 2.4
	 */
	@Override
	public FacadeResult<ResGetPhoneCall> getPhoneCall(ReqGetPhoneCall reqGetPhoneCall) {
		
		FacadeResult<ResGetPhoneCall> result = new FacadeResult<ResGetPhoneCall>();
		
		try {
			ResGetPhoneCall resGetPhoneCall = this.phoneCallService.getPhoneCall(reqGetPhoneCall);
			result.setObject(resGetPhoneCall);
			return result;
		} catch (BussinessException e) {
			log.error("获取被拨打的电话出现错误！原因：" + e.getErrorMessage());
			result.setCode(e.getCode());
			result.setMessage(e.getErrorMessage());
			return result;
		} catch (Exception e2){
			log.error("获取被拨打的电话出现错误！原因：" + e2.getMessage(),e2);
			result.setCode(500);
			result.setMessage("服务器异常");
			return result;
		}
	}
	
}
